<!--
 * @Description: 信息发布
 * @Author: qingyang
 * @Date: 2023-07-07 16:54:58
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-07 17:19:15
-->
<template>
  <div class="info-list-container">
    <CPageTable @register="registerPage" v-model:search-form="searchForm">
      <template #headerRightButton>
        <el-button type="primary" @click="addTask" v-if="hasAddPermission()"
          ><CIcon type="c-xian-xinjian" class="g-mr4"></CIcon>新增</el-button
        >
      </template>
      <template #title="{ scope }">
        <span class="top-tag" v-if="scope.row.placedTop">置顶</span>
        <CLink @click="seeDetail(scope.row)">{{ empty(scope.row.title) }}</CLink>
      </template>

      <template #fileSlot="{ scope }">
        {{ !scope.row.attachmentNum ? '无' : scope.row.attachmentNum + '个' }}
      </template>
      <template #sendType="{ scope }">
        {{ releaseTypeEnumMap[scope.row.sendType] }}
      </template>
      <template #status="{ scope }">
        <el-tag
          class="g-tag"
          :type="scope.row.releaseStatus === 'PUBLISHED' ? 'success' : 'info'"
          >{{ empty(releaseStatusTypeEnum[scope.row.releaseStatus]) }}</el-tag
        >
      </template>
      <template #releaseTime="{ scope }">
        {{ scope.row.releaseStatus === 'PUBLISHED' ? empty(scope.row.releaseTime) : '-' }}
      </template>
      <template #op="{ scope }">
        <CTableButtonGroup :buttons-config="getTableButtonsConfig(scope.row)" />
      </template>
    </CPageTable>
    <AddInfo
      :id="selectId"
      v-model:visible="addOrEditTaskVisible"
      :type="module"
      @refresh="refresh"
    ></AddInfo>
    <Preview v-model:visible="dialogVisible" :info="dialogMain"></Preview>
  </div>
</template>
<script lang="ts">
export default {
  name: 'InformationNotice'
};
</script>
<script lang="ts" setup>
import { reactive } from 'vue';
import { CPageTable, useCPageTable, CPageTableProps } from '@/components/CPageTable';
import { useInfoList } from './useInfoList';
import { empty } from '@/utils/index';
import { releaseStatusTypeEnum, releaseTypeEnumMap } from '@/constants/enum';
import AddInfo from './components/AddInfo/index.vue';

import Preview from '@/businessComponents/Preview/index.vue';
import { GetReleaseListResultModel } from '@/apis/system/model/informationModel';
import { CTableButtonGroup } from '@/components/CTable';
import type { TableButtonItem } from '@/components/CTable';
import { CLink } from '@/components/CLink/index';
import { useInfo } from './useInfo';
import { hasBtnPermission } from '@/directives/btnPermission';
const { searchOptions, searchForm, tableOptions, tableFieldsSetting, title, module } =
  useInfoList();
const options = reactive<CPageTableProps>({
  title,
  searchOptions,
  tableOptions,
  tableFieldsSetting,
  enableFieldsSetting: true
});

const [registerPage, { refresh }] = useCPageTable(options);
const {
  handleDel,
  addTask,
  edit,
  handleTop,
  handleWithdraw,
  seeDetail,
  handleRelease,
  cancelTop,
  dialogVisible,
  selectId,
  dialogMain,
  addOrEditTaskVisible
} = useInfo(refresh);

const hasAddPermission = () => {
  let btnPermission = '';
  if (module == 'SYSTEM_NOTICE') {
    btnPermission = 'INFORMATION_NOTICE_EDIT';
  } else if (module == 'POLICY') {
    btnPermission = 'INFORMATION_POLICY_EDIT';
  } else {
    btnPermission = 'INFORMATION_INSTITUTION_EDIT';
  }
  return hasBtnPermission(btnPermission);
};
/**
 * 配置表格的按钮
 * @param row 表格行数据
 */
function getTableButtonsConfig(row: GetReleaseListResultModel): TableButtonItem[] {
  let arr: TableButtonItem[] = [];
  let btnPermission = '';
  if (module == 'SYSTEM_NOTICE') {
    btnPermission = 'INFORMATION_NOTICE_EDIT';
  } else if (module == 'POLICY') {
    btnPermission = 'INFORMATION_POLICY_EDIT';
  } else if (module == 'INSTITUTION') {
    btnPermission = 'INFORMATION_INSTITUTION_EDIT';
  }
  if (hasBtnPermission(btnPermission)) {
    // 有按钮权限才去添加按钮
    if (row.releaseStatus == 'DRAFT') {
      arr.push({
        text: '发布',
        btnPermission,
        onClick: () => handleRelease(row)
      });
      arr = arr.concat([
        {
          text: '编辑',
          btnPermission,
          onClick: () => edit(row)
        },
        {
          text: '删除',
          btnPermission,
          onClick: () => handleDel(row)
        }
      ]);
    } else {
      arr.push({
        text: '撤回',
        btnPermission,
        onClick: () => handleWithdraw(row)
      });
      if (row.placedTop) {
        arr.push({
          text: '取消置顶',
          btnPermission,
          onClick: () => cancelTop(row)
        });
      } else {
        arr.push({
          text: '置顶',
          btnPermission,
          onClick: () => handleTop(row)
        });
      }
    }
  }

  return arr;
}
</script>

<style lang="scss" scoped>
.info-list-container {
  min-height: 80%;
  .top-tag {
    display: inline-block;
    width: 24px;
    height: 20px;
    background: #03385d;
    color: #ffffff;
    padding: 0 6px;
    margin-right: 5px;
    font-size: 12px;
    border-radius: 2px;
    line-height: 20px;
  }
  .rangeTip {
    cursor: pointer;
    color: #004cb3;
  }
  .rangeTip:hover {
    color: #004cb3;
  }
}
:deep(.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner) {
  background-color: var(--el-checkbox-checked-bg-color);
}

.ding-title {
  font-size: 14px;
  font-family: SourceHanSansCN-Bold, SourceHanSansCN;
  font-weight: bold;
  color: #212529;
}

.tree {
  position: relative;
  width: 100%;
  margin-top: 15px;
}
.rt {
  position: relative;
  z-index: 1;
  background: transparent;
  // width: 100%;
  margin-left: 12px;
  // padding-top: 8px;
  :deep(.el-tree-node__content) {
    height: 36px;
    border: 1px solid transparent;
    border-radius: 4px;
    font-size: 14px;
    font-weight: 400;
    color: #51596c;
    .handle-button {
      display: none;
    }
  }
}
.g-tree-bg {
  position: absolute;
  z-index: 0;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  // background: #f6f9fa;
  background: #fff;
  padding-right: 25px;
  border-radius: 4px;
  // padding-top: 8px;
  line-height: 36px;
  div {
    height: 36px;
    width: 100%;
    border-radius: 4px;
    margin-left: 12px;
    margin-right: 12px;
  }
  div:nth-child(even) {
    height: 36px;
    width: 100%;
    // background: #fff;
    background: #f6f9fa;
    border-radius: 4px;
    margin-left: 12px;
    margin-right: 12px;
  }
}

:deep(.el-tree-node__content):hover {
  background-color: transparent;
}
</style>
